package cn.emergentdesign.demo.bowling;

public class Frame {
    private int bonusCount = 0;
    private int score = 0;
    private int rollIndex = 0;

    public void roll(int pins) {
        rollIndex++;
        score += pins;
        checkSpare();
        checkStrike(pins);
    }

    public void addBonus(int pins) {
        if (bonusCount > 0){
            score += pins;
            bonusCount--;
        }
    }

    public int getScore() {
        return score;
    }

    public void checkStrike(int pins) {
        if (pins == 10){
            bonusCount = 2;
            rollIndex = 2;
        }
    }

    private void checkSpare() {
        if (rollIndex == 2 && score == 10){
            bonusCount = 1;
        }
    }

    public boolean isFinished() {
        return rollIndex == 2;
    }

    public boolean bonusUsed() {
        return bonusCount == 0;
    }
}
